package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.ProductSendOutDetailBatchMapper;
import com.zhijian.medical.entity.po.ProductSendOutDetailBatch;
import com.zhijian.medical.entity.vo.request.ProductSendOutDetailBatchSearchVo;
import com.zhijian.medical.entity.vo.response.ProductSendOutDetailBatchVo;
import com.zhijian.medical.service.ProductSendOutDetailBatchService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 产品订单发货，批次号-数量表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-03-21 15:57:40
 */
@Slf4j
@Service
public class ProductSendOutDetailBatchServiceImpl implements ProductSendOutDetailBatchService {

    @Autowired
    private ProductSendOutDetailBatchMapper mapper;

    @Override
    public ProductSendOutDetailBatch selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ProductSendOutDetailBatchVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(ProductSendOutDetailBatch productSendOutDetailBatch) {
        Date now = new Date();
        productSendOutDetailBatch.setStartTime(now);
        productSendOutDetailBatch.setCreateTime(now);
        productSendOutDetailBatch.setUpdateTime(now);
        return mapper.insert(productSendOutDetailBatch) > 0 ? productSendOutDetailBatch.getId() : 0L;
    }

    @Override
    public boolean updateByPrimaryKeySelective(ProductSendOutDetailBatch record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean update(ProductSendOutDetailBatch productSendOutDetailBatch) {
        Date now = new Date();
        productSendOutDetailBatch.setUpdateTime(now);
        return mapper.updateByPrimaryKey(productSendOutDetailBatch) != 0;
    }

    @Override
    public Integer count(ProductSendOutDetailBatchSearchVo productSendOutDetailBatchSearchVo) {
        return mapper.count(productSendOutDetailBatchSearchVo);
    }

    @Override
    public List<ProductSendOutDetailBatchVo> selectList(ProductSendOutDetailBatchSearchVo productSendOutDetailBatchSearchVo) {
        return mapper.selectList(productSendOutDetailBatchSearchVo);
    }

    @Override
    public Page<ProductSendOutDetailBatchVo> selectPage(ProductSendOutDetailBatchSearchVo productSendOutDetailBatchSearchVo) {
        //先直接查总数
        int count = count(productSendOutDetailBatchSearchVo);
        //分页
        Page<ProductSendOutDetailBatchVo> page = new Page<>(true, count, productSendOutDetailBatchSearchVo.getPageNum(), productSendOutDetailBatchSearchVo.getPageSize());
        productSendOutDetailBatchSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ProductSendOutDetailBatchVo> responseVos = this.selectList(productSendOutDetailBatchSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public Integer sumQuantityByProductId(Long productId) {
        return mapper.sumQuantityByProductId(productId);
    }

}